<?php
	require("Util.php");
	
	$err = false;
	if (isset($_POST["createBtn"])) {
		//create the folder for the book
		$folder_name = cleanFilename(strtolower($_POST["templateFile"]));
		if ((strlen($folder_name) > 0) && (!file_exists($GLOBALS['uploaddir']."/".$folder_name))) {
			makeFolder($GLOBALS['uploaddir']."/".$folder_name, 0777);

			//create the folder for the unprocessed file
			if (!file_exists($GLOBALS['uploaddir']."/".$folder_name."/unprocessed")) {
				makeFolder($GLOBALS['uploaddir']."/".$folder_name."/unprocessed", 0777);
			}

			//create the folder for the repository
			if (!file_exists($GLOBALS['uploaddir']."/".$folder_name."/repository")) {
				makeFolder($GLOBALS['uploaddir']."/".$folder_name."/repository", 0777);
			}
			
			//create the folder for the image in wiki tooltiptext
			if (!file_exists($GLOBALS['uploaddir']."/".$folder_name."/images")) {
				makeFolder($GLOBALS['uploaddir']."/".$folder_name."/images", 0777);
			}
			
			//create the folder for the text in wiki tooltiptext
			if (!file_exists($GLOBALS['uploaddir']."/".$folder_name."/xmls")) {
				makeFolder($GLOBALS['uploaddir']."/".$folder_name."/xmls", 0777);
			}
			copy("ExampleBooks/Designer.js", $GLOBALS['uploaddir']."/".$folder_name."/Designer.js");
			copy("ExampleBooks/Designer.swf", $GLOBALS['uploaddir']."/".$folder_name."/Designer.swf");
			copy("ExampleBooks/Designer.html",$GLOBALS['uploaddir']."/".$folder_name."/Designer.html");
			
			echo "<script type='text/javascript'>";
			echo "location.href = 'bookdir/".$folder_name."/Designer.html';";
			echo "</script>";
		} else {
			$err = true;
		}
	}
?>
<html>
<head>
<title>Creating Realistic Books</title>
</head>
<body style="font-family:Arial, Helvetica, sans-serif;font-size:14px;">
<div>
<h1>List of books:</h1>
<?php
	if ($handle = opendir('bookdir')) {
     while (false !== ($file = readdir($handle))) {
        $dir = "bookdir/".$file;
        if(is_dir($dir) && $file != '.' && $file !='..' )
        {
					echo '<li style="padding:10px;">';
					echo $file.' : <span style="font-size:12px;">';
					if (file_exists($GLOBALS['uploaddir']."/".$file."/Designer.html"))
						echo '<a href="bookdir/'.$file.'/Designer.html">Modify</a> &bull; ';
					if (file_exists($GLOBALS['uploaddir']."/".$file."/index.html"))
						echo '<a href="bookdir/'.$file.'/index.html">View</a> &bull; ' ;
					echo '<a href="zipBook.php?templateFile='.$file.'">Download</a>';
					echo '</span></li>';
		}
    }
    closedir($handle);
	}
?>
<li style="padding:10px;">Create a new book:</li>
<div style="text-indent: 40px;">
<?php 
		if ($err == true) {
			echo "<p style='color:red;'>Please choose a different BookID.</p>"; 
		}
?>
<form id="formFile" action="<?php echo $PHP_SELF; ?>" name="formFile" method="post" enctype="multipart/form-data">
<label>Enter BookID <span style="font-style:italic;font-size:12px;">(max. 20 characters)</span>:
<input type="text" name="templateFile" maxlength="20" value="<?php echo $_GET["templateFile"]; ?>" /></label>
<input type="submit" name="createBtn" id="createBtn" value="Create Book" style="width:100px;font-family:Arial, Helvetica, sans-serif;font-size:12px;" />
</form>
</div>
</div>
</body>
</html>